import { DrawType } from '@/utils/json2image.js';
export const getCustCertJson = (dataInfo, json2image) => {
  let jsonData = [];
  if (!dataInfo) {
    return jsonData;
  }
  let bgItem = {
    type: DrawType.Image,
    url: dataInfo.bgImgUrl,
    left: 0,
    top: 0,
    width: json2image.width,
    height: json2image.height
  };
  jsonData.push(bgItem);
  if (!dataInfo.customizeControl) {
    return jsonData;
  }
  for (let item of dataInfo.customizeControl) {
    if (item.type == 1) {
      const imgUrlMap = {
        1: dataInfo.qrCodeUrl,
        2: dataInfo.homeQrcode
      };
      let imgUrl = imgUrlMap[item.redirectTo] || item.img;
      const qrItem = {
        type: DrawType.Image,
        url: imgUrl,
        left: item.left,
        top: item.top,
        width: item.width,
        height: item.height
      };
      jsonData.push(qrItem);
    } else if (item.type == 2) {
      const textBoxItem = {
        type: DrawType.TextBox,
        content: item.name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(textBoxItem);
    } else if (item.type == 3) {
      let left = item.left + item.width / 2 - 46;
      const avatarItem = {
        type: DrawType.Image,
        url: dataInfo.headimgurl,
        left: left,
        top: item.top,
        width: 92,
        height: 92,
        radius: '50%'
      };
      jsonData.push(avatarItem);
      let name = dataInfo.nickname;
      const nicknameBoxItem = {
        type: DrawType.TextBox,
        content: name,
        left: item.left,
        top: item.top + 92,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(nicknameBoxItem);
    } else if (item.type == 6) {
      let left = item.left + item.width / 2 - 46;
      const avatarItem = {
        type: DrawType.Image,
        url: dataInfo.headimgurl,
        left: left,
        top: item.top,
        width: 92,
        height: 92,
        radius: '50%'
      };
      jsonData.push(avatarItem);
    } else if (item.type == 7) {
      let name = item.nameTypeShow == 2 && dataInfo.realName ? dataInfo.realName : dataInfo.nickname;
      const nicknameBoxItem = {
        type: DrawType.TextBox,
        content: name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(nicknameBoxItem);
    } else if (item.type == 8) {
      const custImgItem = {
        type: DrawType.Image,
        url: item.img,
        left: item.left,
        top: item.top,
        width: item.width,
        height: item.height
      };
      jsonData.push(custImgItem);
    } else if (item.type == 9) {
      const textBoxItem = {
        type: DrawType.TextBox,
        content: item.name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(textBoxItem);
    } else if (item.type == 10) {
      let name = `${dataInfo.number}`;
      const textBoxItem = {
        type: DrawType.TextBox,
        content: name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(textBoxItem);
    }
  }
  return jsonData;
};
export const getVerCertJson = (dataInfo, json2image) => {
  let jsonData = [];
  if (!dataInfo) {
    return jsonData;
  }
  const width = json2image.width;
  const height = json2image.height;
  let bgItem = {
    type: DrawType.Image,
    url: dataInfo.bgImgUrl,
    left: 0,
    top: 0,
    width: width,
    height: height
  };
  jsonData.push(bgItem);
  let titleTextItem = {
    type: DrawType.Text,
    content: dataInfo.title,
    fontSize: 60,
    weight: 600,
    textAlign: 'center',
    color: '#333',
    left: width / 2,
    top: 130
  };
  jsonData.push(titleTextItem);
  if (dataInfo.enableAvatarShow == 1) {
    let avatarItem = {
      type: DrawType.Image,
      url: dataInfo.headimgurl,
      left: width / 2 - 60,
      top: 290,
      width: 120,
      height: 120,
      radius: '50%'
    };
    jsonData.push(avatarItem);
  }
  if (dataInfo.nameTypeShow == 1) {
    let name = dataInfo.realName ? dataInfo.realName : dataInfo.nickname;
    let nameTextItem = {
      type: DrawType.Text,
      content: name,
      fontSize: 32,
      textAlign: 'center',
      weight: 600,
      color: '#333',
      left: width / 2,
      top: 430
    };
    jsonData.push(nameTextItem);
  }
  let prodNameItem = {
    type: DrawType.Text,
    content: dataInfo.prodName,
    fontSize: 30,
    weight: 600,
    textAlign: 'center',
    color: '#333',
    left: width / 2,
    top: 490,
    maxWidth: width - 120,
    maxLine: 2,
    lineHeight: 42,
    eplision: '...》'
  };
  jsonData.push(prodNameItem);
  //是否显示课时数 type == 2才显示
  if (dataInfo.defineShowContent && dataInfo.defineShowContent.length && dataInfo.defineShowContent.some(i => i == 2)) {
    let courseNumTextItem = {
      type: DrawType.Text,
      content: dataInfo.completeLessonCount,
      fontSize: 48,
      weight: 600,
      textAlign: 'center',
      color: '#333',
      left: width / 2,
      top: 633
    };
    let courseTextItem = {
      type: DrawType.Text,
      content: '课时数',
      fontSize: 26,
      textAlign: 'center',
      color: '#999',
      left: width / 2,
      top: 715
    };
    jsonData.push(courseNumTextItem);
    jsonData.push(courseTextItem);
  }
  let offSetY = 820;
  for (let i = 0; i < dataInfo.wordArr.length; i++) {
    let wordTextItem = {
      type: DrawType.Text,
      content: dataInfo.wordArr[i],
      fontSize: 28,
      lineHeight: 40,
      textAlign: 'center',
      color: '#333',
      left: width / 2,
      top: offSetY + i * 40
    };
    jsonData.push(wordTextItem);
  }
  // //获取二维码和证书信息的总高度
  // let qrBoxHeight = 0;
  // let qrTextOptions = {
  //     content: dataInfo.qrCodeText,
  //     fontSize: 20,
  //     lineHeight: 24,
  //     maxWidth: 170,
  // }
  // let textHeight = json2image.getTextHeight(qrTextOptions)
  // qrBoxHeight = 140 + textHeight + 12;

  // let certInfoBoxHeight = 0;
  // //发证机构高度
  // if (dataInfo.enableInstitution == 1) {
  //   let institutionOptions = {
  //     content: dataInfo.institutionName,
  //     fontSize: 22,
  //     lineHeight: 26,
  //     maxWidth:300,
  //   }
  //   let institutionTextHeight = json2image.getTextHeight(institutionOptions)
  //   certInfoBoxHeight += institutionTextHeight;
  // }
  // //发证日期高度
  // if (dataInfo.enableGrantTime == 1) {
  //   let timeOptions = {
  //     content: dataInfo.createdAt,
  //     fontSize: 20,
  //     lineHeight: 24,
  //     maxWidth:300,
  //   }
  //   let timeTextHeight = json2image.getTextHeight(timeOptions)
  //   certInfoBoxHeight = certInfoBoxHeight + timeTextHeight + 14;
  // }
  // //发证
  // if (dataInfo.enableNumber == 1) {
  //   let numberOptions = {
  //     content: dataInfo.number,
  //     fontSize: 20,
  //     lineHeight: 24,
  //     maxWidth:300,
  //   }
  //   let numberTextHeight = json2image.getTextHeight(numberOptions)
  //   certInfoBoxHeight = certInfoBoxHeight + numberTextHeight + 8;
  // }
  // console.log(qrBoxHeight, certInfoBoxHeight)
  //显示二维码或者证书信息，如果只有一个显示则居中
  let isShowQr = dataInfo.enableShowQrCode == 1;
  let isShowCertInfo = dataInfo.enableInstitution == 1 || dataInfo.enableGrantTime == 1 || dataInfo.enableNumber == 1 || dataInfo.enableStamp != 0;
  if (isShowQr) {
    const imgUrlMap = {
      1: dataInfo.qrCodeUrl,
      2: dataInfo.homeQrcode,
      3: dataInfo.customizeQrCode
    };
    //如果两个都显示，则居左，如果显示一个则居中
    let left = isShowCertInfo ? (width - 540) / 2 + 15 : width / 2 - 70;
    let imgUrl = imgUrlMap[dataInfo.redirectTo];
    let qrItem = {
      type: DrawType.Image,
      url: imgUrl,
      left: left,
      top: 980,
      width: 140,
      height: 140
    };
    let qrTextLeft = isShowCertInfo ? (width - 540) / 2 + 170 / 2 : width / 2;
    let qrTextItem = {
      type: DrawType.Text,
      content: dataInfo.qrCodeText,
      fontSize: 20,
      lineHeight: 24,
      textAlign: 'center',
      color: '#666',
      left: qrTextLeft,
      maxWidth: 170,
      top: 1132
    };
    jsonData.push(qrItem);
    jsonData.push(qrTextItem);
  }
  //显示徽章图标
  if (dataInfo.enableStamp != 0) {
    //显示二维码的话 -- 左侧距离 = 左侧间距宽度105 + 二维码盒子宽度170 + 间距70 + （右侧盒子宽度300 - 徽章盒子宽度120） / 2
    // 隐藏二维码的话 --左侧距离 = 总宽度/2 - 徽章盒子宽度/2
    let stampLeft = isShowQr ? (width - 540) / 2 + 170 + 70 + (300 - 120) / 2 : width / 2 - 60;
    let stampItem = {
      type: DrawType.Image,
      url: dataInfo.stampImg,
      left: stampLeft,
      top: 980,
      width: 120,
      height: 120
    };
    jsonData.push(stampItem);
  }
  let certInfoBoxTop = 990;
  let certInfoBoxLeft = isShowQr ? (width - 540) / 2 + 170 + 70 : width / 2 - 150;
  if (dataInfo.enableInstitution == 1) {
    let institutionNameItem = {
      type: DrawType.Text,
      content: dataInfo.institutionName,
      fontSize: 22,
      lineHeight: 26,
      maxWidth: 300,
      color: '#333',
      left: certInfoBoxLeft,
      top: certInfoBoxTop
    };
    jsonData.push(institutionNameItem);
    let institutionTextHeight = json2image.getTextHeight(institutionNameItem);
    certInfoBoxTop += institutionTextHeight;
  }
  if (dataInfo.enableGrantTime == 1) {
    certInfoBoxTop += 14;
    let timeItem = {
      type: DrawType.Text,
      content: dataInfo.createdAt,
      fontSize: 20,
      lineHeight: 24,
      maxWidth: 300,
      color: '#666',
      left: certInfoBoxLeft,
      top: certInfoBoxTop
    };
    jsonData.push(timeItem);
    certInfoBoxTop += 24;
  }
  if (dataInfo.enableNumber == 1 && dataInfo.number) {
    certInfoBoxTop += 8;
    let numberItem = {
      type: DrawType.Text,
      content: dataInfo.number,
      fontSize: 20,
      lineHeight: 24,
      maxWidth: 300,
      color: '#666',
      left: certInfoBoxLeft,
      top: certInfoBoxTop
    };
    jsonData.push(numberItem);
  }

  return jsonData;
};
export const getHorCertJson = (dataInfo, json2image) => {
  let jsonData = [];
  if (!dataInfo) {
    return jsonData;
  }
  const width = json2image.width;
  const height = json2image.height;
  let bgItem = {
    type: DrawType.Image,
    url: dataInfo.bgImgUrl,
    left: 0,
    top: 0,
    width: width,
    height: height
  };
  jsonData.push(bgItem);
  let titleTextItem = {
    type: DrawType.Text,
    content: dataInfo.title,
    fontSize: 20,
    weight: 600,
    textAlign: 'center',
    color: '#333',
    left: width / 2,
    top: 46
  };
  jsonData.push(titleTextItem);
  if (dataInfo.enableAvatarShow == 1) {
    let avatarItem = {
      type: DrawType.Image,
      url: dataInfo.headimgurl,
      left: width / 2 - 25,
      top: 102,
      width: 50,
      height: 50,
      radius: '50%'
    };
    jsonData.push(avatarItem);
  }
  if (dataInfo.nameTypeShow == 1) {
    let name = dataInfo.realName ? dataInfo.realName : dataInfo.nickname;
    let nameTextItem = {
      type: DrawType.Text,
      content: name,
      fontSize: 16,
      textAlign: 'center',
      weight: 600,
      color: '#333',
      left: width / 2,
      top: 160
    };
    jsonData.push(nameTextItem);
  }
  let prodNameItem = {
    type: DrawType.Text,
    content: dataInfo.prodName,
    fontSize: 18,
    weight: 600,
    textAlign: 'center',
    color: '#333',
    left: width / 2,
    top: 184,
    maxWidth: width - 200,
    lineHeight: 22
  };
  jsonData.push(prodNameItem);
  let offsetY = 184;
  let prodNameHeight = json2image.getTextHeight(prodNameItem);
  offsetY += prodNameHeight;
  //是否显示课时数 type == 2才显示
  if (dataInfo.defineShowContent && dataInfo.defineShowContent.length && dataInfo.defineShowContent.some(i => i == 2)) {
    offsetY += 20;
    let courseNumTextItem = {
      type: DrawType.Text,
      content: dataInfo.completeLessonCount,
      fontSize: 24,
      weight: 600,
      textAlign: 'center',
      color: '#333',
      left: width / 2,
      top: offsetY
    };
    offsetY += 29;
    let courseTextItem = {
      type: DrawType.Text,
      content: '课时数',
      fontSize: 16,
      textAlign: 'center',
      color: '#999',
      left: width / 2,
      top: offsetY
    };
    offsetY += 20;
    jsonData.push(courseNumTextItem);
    jsonData.push(courseTextItem);
  }
  offsetY += 20;
  for (let i = 0; i < dataInfo.wordArr.length; i++) {
    let wordTextItem = {
      type: DrawType.Text,
      content: dataInfo.wordArr[i],
      fontSize: 18,
      lineHeight: 26,
      textAlign: 'center',
      color: '#333',
      left: width / 2,
      top: offsetY + i * 26
    };
    jsonData.push(wordTextItem);
  }

   let isShowQr = dataInfo.enableShowQrCode == 1;
   if (isShowQr) {
     const imgUrlMap = {
       1: dataInfo.qrCodeUrl,
       2: dataInfo.homeQrcode,
       3: dataInfo.customizeQrCode
     };
     let imgUrl = imgUrlMap[dataInfo.redirectTo];
     let qrItem = {
       type: DrawType.Image,
       url: imgUrl,
       left: (width - 600) / 2,
       top: 400,
       width: 76,
       height: 76
     };
     let qrTextLeft = (width - 600) / 2 + 76 + 10;
     let qrTextOptions = {
      type: DrawType.Text,
      content: dataInfo.qrCodeText,
      fontSize: 16,
      lineHeight: 20,
      color: '#666',
      left: qrTextLeft,
      maxWidth: 160,
    };
     let qrTextHeight = json2image.getTextHeight(qrTextOptions);
      let qrTextItem = {
        ...qrTextOptions,
        top: 400 + (76 / 2 - qrTextHeight / 2)
      };
     jsonData.push(qrItem);
     jsonData.push(qrTextItem);
   }
    //显示徽章图标
  if (dataInfo.enableStamp != 0) {
    //左侧距离 = 左侧间距宽度75 + (600 - 二维码盒子宽度76 + 二维码文字宽度170) + （右侧盒子宽度300 - 徽章盒子宽度120） / 2
    let stampLeft = (width - 600) / 2 + 76 + 170 + (600 - 76 - 170) / 2 ;
    let stampItem = {
      type: DrawType.Image,
      url: dataInfo.stampImg,
      left: stampLeft,
      top: 400,
      width: 96,
      height: 96
    };
    jsonData.push(stampItem);
  }
  if (dataInfo.enableInstitution == 1) {
    let institutionNameItem = {
      type: DrawType.Text,
      content: dataInfo.institutionName,
      fontSize: 16,
      lineHeight: 20,
      textAlign:'right',
      color: '#333',
      left: width - (width - 600) / 2,
      top: 420
    };
    jsonData.push(institutionNameItem);
  }
  if (dataInfo.enableGrantTime == 1) {
    let timeItem = {
      type: DrawType.Text,
      content: dataInfo.createdAt,
      fontSize: 16,
      lineHeight: 20,
      textAlign:'right',
      color: '#666',
      left: width - (width - 600) / 2,
      top: 444
    };
    jsonData.push(timeItem);
  }
  if (dataInfo.enableNumber == 1 && dataInfo.number) {
    let numberItem = {
      type: DrawType.Text,
      content: dataInfo.number,
      fontSize: 16,
      lineHeight: 20,
      textAlign:'right',
      color: '#666',
      left: width - (width - 600) / 2,
      top: 468
    };
    jsonData.push(numberItem);
  }
  return jsonData;
};
